{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "%load_ext tensorboard\n",
    "import datetime, os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist = tf.keras.datasets.fashion_mnist\n",
    "\n",
    "(x_train_1, y_train), (x_test_1, y_test) = mnist.load_data()\n",
    "x_train_1, x_test_1 = x_train_1 / 255.0, x_test_1 / 255.0\n",
    "\n",
    "x_train = x_train_1.reshape(60000,28,28,1)\n",
    "x_test = x_test_1.reshape(10000,28,28,1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANgAAABiCAYAAAAycR3+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAQB0lEQVR4nO2de6wV1b3HPz+OouITBBURBRVF4/tBfVutWNLEt1XUmDbW1JjW3JvUBNt/7k1MI4nm5l6jMVGvVmPThynamohKCEltVESBiOVQQOLj4IlHRPGFIrr6xznfmbWH2fvsc87Mnr05v88/Z5+ZNTNrz2/P+j3Wb/3GQgg4jlMOY6rugOPszPgD5jgl4g+Y45SIP2COUyL+gDlOifgD5jglMqIHzMzmmNm/zGy9md1RVKecanG5FocNdx7MzLqAtcBsoAdYBlwXQlhdXPecVuNyLZaRaLBZwPoQwoYQwjbgj8BlxXTLqRCXa4HsMoJjpwDvRf/3AN9rdICZjfa0kU0hhElVd2IQ2kquu+++OwCHHnooAJs3b072ffnllwDICoutsT322AOA8ePHA/DVV18B8MEHHyRtvv3226K6WVeuI3nALGfbDjfazH4O/HwE19mZeKfqDjRBqXI1S0/fjHsybdo0AO677z4Annzyy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hR4cimLmb2IfAFsGmwtm3IREbe78NCCJOK6Ew74XKtL9eWPmAAZvZaCOG0ll60ADq1362iU+9P2f12E9FxSsQfMMcpkSoesAcruGYRdGq/W0Wn3p9S+91yH8xxRhNuIjpOibTsATOzOWb2LzNbb2ZtW47ZzKaa2RIz6zazf5rZfwxsn2Bmi8xs3cDf8VX3tV3oBNlWJdeWmIhm1gWsBWYDPcAy4LoQwurSLz5EBmoBTg4hLDezvYHX6S8f/VNgcwhh/sCPaHwIYV6FXW0LOkW2Vcm1VRpsFrA+hLAhhLAN+CP9NdDbDq/ZPmQ6QrZVybVVD9gUIC7M3jOwra1pVLMdaFnN9jan42TbSrm26gHLq7/X1uHLbM32qvvTxnSUbFst11Y9YD3A1Oj/Q4D367StnEY12wf2163ZPgrpGNlWIddWPWDLgBlmNt3MxgJz6a+B3nZ4zfYh0xGyrUquLZtoNrMfAf8LdAGPhBB+25ILDxEzOwd4EVhF/6tVob9m+1Lgz8ChDNRsDyFszj3JKKMTZFuVXD2Tw3FKxDM5HKdE/AFznBLxB8xxSsQfMMcpEX/AHKdE/AFznBLxB8xxSsQfMMcpkX8DQSU5YCjh/5MAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot\n",
    "for i in range(2):\n",
    "    pyplot.subplot(330 + 1 + i)\n",
    "    pyplot.imshow(x_train_1[i], cmap=pyplot.get_cmap('gray'))\n",
    "pyplot.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_6\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_10 (Conv2D)           (None, 28, 28, 32)        320       \n",
      "_________________________________________________________________\n",
      "max_pooling2d_10 (MaxPooling (None, 14, 14, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_11 (Conv2D)           (None, 14, 14, 64)        18496     \n",
      "_________________________________________________________________\n",
      "max_pooling2d_11 (MaxPooling (None, 7, 7, 64)          0         \n",
      "_________________________________________________________________\n",
      "flatten_4 (Flatten)          (None, 3136)              0         \n",
      "_________________________________________________________________\n",
      "dense_4 (Dense)              (None, 256)               803072    \n",
      "_________________________________________________________________\n",
      "dense_5 (Dense)              (None, 84)                21588     \n",
      "_________________________________________________________________\n",
      "dense_6 (Dense)              (None, 10)                850       \n",
      "=================================================================\n",
      "Total params: 844,326\n",
      "Trainable params: 844,326\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Conv2D, Dense, MaxPool2D, Dropout, Flatten, Reshape\n",
    "from tensorflow.keras.constraints import max_norm\n",
    "from tensorflow.keras import regularizers\n",
    "from tensorflow.keras import initializers\n",
    "\n",
    "model = Sequential()\n",
    "model.add(Conv2D(kernel_initializer=initializers.GlorotNormal(seed=12),  \n",
    "                 bias_initializer=initializers.GlorotUniform(seed=12),\n",
    "    filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=(28, 28, 1)))\n",
    "model.add(MaxPool2D(padding='same'))\n",
    "model.add(Conv2D(kernel_initializer=initializers.he_normal(seed=12),  \n",
    "                 bias_initializer=initializers.he_uniform(seed=12),\n",
    "    filters=64, kernel_size=(3,3), padding='same', activation='relu'))\n",
    "model.add(MaxPool2D(padding='same'))\n",
    "model.add(Flatten())\n",
    "model.add(Dense(256, activation='relu',\n",
    "         kernel_initializer=initializers.lecun_normal(seed=12),  \n",
    "                 bias_initializer=initializers.lecun_uniform(seed=12)))\n",
    "model.add(Dense(84, activation='relu'))\n",
    "model.add(Dense(10, activation='linear'))\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples\n",
      "Epoch 1/2\n",
      "60000/60000 [==============================] - 22s 359us/sample - loss: 0.8663 - accuracy: 0.6963\n",
      "Epoch 2/2\n",
      "60000/60000 [==============================] - 22s 371us/sample - loss: 0.4749 - accuracy: 0.8270\n",
      "10000/1 - 1s - loss: 0.4596 - accuracy: 0.8278\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.4694333761930466, 0.8278]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train, y_train, batch_size=1000, epochs=2)\n",
    "\n",
    "model.evaluate(x_test,  y_test, batch_size=1000, verbose=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On the next step, let's export the model to json configuration file and weights to the hdf5 file to be parsed on the Kotlin DL side."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_weights(\"C:\\\\zaleslaw\\\\home\\\\models\\\\tests\\\\initializers\\\\mnist_weights_only.h5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# serialize model to JSON\n",
    "model_json = model.to_json()\n",
    "with open(\"C:\\\\zaleslaw\\\\home\\\\models\\\\tests\\\\initializers\\\\modelConfig.json\", \"w\") as json_file:\n",
    "    json_file.write(model_json)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
